package com.lzp.customview.dashboard

import android.content.Context
import android.graphics.Canvas
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Paint.*
import android.graphics.RectF
import android.graphics.Shader
import android.graphics.Shader.*
import android.util.AttributeSet
import android.view.View

/**
 * @describe
 * @author: lixiaopeng
 * @Date: 2019-08-19
 */
class DashBoardView : View {

    private var centerX = 0
    private var centerY = 0

    private val startDegree = 145f //开始的角度
    private val swipeDegree = 250f //扫过的角度
    private val radios = 250 //半径
    private var viewWidth = 0
    private var viewHeight = 0

    /***外圈 */
    private val outStrokeWidth = 15f
    private val outStartColor = 0x33ffffff.toInt() //外圈渐变开始色
    private val outEndColor = 0xffffffff.toInt() //外圈渐变结束色
    private var outPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    var rect = RectF()

    //刻度
    private val scalePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val scaleColor = 0xffa1b2c3.toInt()
    private val scaleStartR = 220//仪表长半径
    private val scaleEndR = 150//仪表短半径
    private val scaleCount = 100
    private val scaleWidth = 3f

    companion object {
        private const val TAG = "DashBoardView"
    }

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        initPaint()
    }

    private fun initPaint() {
        viewWidth = 2 * radios
        val remianDegree = (360 - swipeDegree) / 2
        val v = Math.toRadians(remianDegree.toDouble())
        viewHeight = radios + (radios * Math.cos(v)).toInt()
        centerX = radios
        centerY = radios

        outPaint.style = Paint.Style.STROKE
        outPaint.strokeWidth = outStrokeWidth
        outPaint.strokeCap = Cap.ROUND
        val shader: Shader = LinearGradient(viewWidth / 2f, viewHeight.toFloat(), viewWidth / 2f, outStrokeWidth.toFloat(), outStartColor, outEndColor, TileMode.MIRROR)
        outPaint.shader = shader
        rect.set(outStrokeWidth, outStrokeWidth, 2 * radios - outStrokeWidth, 2 * radios - outStrokeWidth)

        scalePaint.color = scaleColor
        scalePaint.style = Paint.Style.STROKE
        scalePaint.strokeWidth = scaleWidth
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.makeMeasureSpec(viewWidth, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(viewHeight, MeasureSpec.EXACTLY))
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawArc(rect, startDegree, swipeDegree, false, outPaint)
        //画刻度
        drawScale(canvas)
    }

    private fun drawScale(canvas: Canvas) {
        //总共的角度是250，所以一刻度是2.5
        val dAngle = swipeDegree / scaleCount * Math.PI / 180
        val startDegree = (90 + 56) * Math.PI / 180

        var lineStartX = 0f
        var lineStartY = 0f
        var lineEndX = 0f
        var lineEndY = 0f
        var rotateDegrees = 0.0
        var sinDegree: Float
        var cosDegree: Float

        repeat(scaleCount) {
            rotateDegrees = startDegree + it * dAngle

            sinDegree = Math.sin(rotateDegrees).toFloat()
            cosDegree = Math.cos(rotateDegrees).toFloat()

            lineStartX = centerX + cosDegree * scaleStartR
            lineStartY = centerY + sinDegree * scaleStartR

            lineEndX = centerX.toFloat() + cosDegree * scaleEndR
            lineEndY = centerY.toFloat() + sinDegree * scaleEndR
            canvas.drawLine(lineStartX, lineStartY, lineEndX, lineEndY, scalePaint)
        }
    }
}